package pk.nak.responses;

import java.io.StringReader;
import java.util.Hashtable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import pk.nak.data.EatOutAttributeValue;



public class EatOutAttributessResponse extends BaseResponse {
	private Hashtable<String, EatOutAttributeValue[]> htAttributes;
	@Override
	public void onSuccess(String response) {
		try{
			this.response = response;
			DocumentBuilderFactory dbfResponse = DocumentBuilderFactory.newInstance();
	        DocumentBuilder dbResponse = dbfResponse.newDocumentBuilder();
	        
	        Document dom = dbResponse.parse(new InputSource(new StringReader(response)));
	        NodeList nl = dom.getElementsByTagName("attribute");
	        
	        this.htAttributes = new Hashtable<String, EatOutAttributeValue[]>();
	        
	        if ( nl != null && nl.getLength() > 0 ){
	        	for ( int i = 0 ; i < nl.getLength(); i ++){
	        		Element ele = (Element)nl.item(i);
	        		String attrName = ele.getAttribute("title");
	        		NodeList nlc = ele.getElementsByTagName("value");
	        		
	        		if ( nlc != null && nlc.getLength() > 0 ){
	        			EatOutAttributeValue[] arr = new EatOutAttributeValue[nlc.getLength()];
	        			
	        			for ( int j = 0 ; j < nlc.getLength() ; j ++ ){
	        				Element elec = (Element)nlc.item(j);
	        				arr[j] = new EatOutAttributeValue(elec.getAttribute("id"),
	        						attrName,
	        						elec.getAttribute("title"),
	        						(int)Integer.parseInt(elec.getAttribute("weight")));
	        			}
		        		this.htAttributes.put(attrName, arr);
	        		}
	        	}
	        }
		}catch(Exception ex){
			System.out.println("-----EXP--------" + ex.getMessage());
		}
	}
	public Hashtable<String, EatOutAttributeValue[]> getHtAttributes() {
		return htAttributes;
	}
}
